package cn.wolfcode.luowowo.search.query;

import lombok.Data;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.io.Serializable;

/**
 * @author 猫教授
 * @date 2019/8/18 19:23
 */
@Data
public class GoldProductSearchQuery implements Serializable{
    private int currentPage=1;//当前页属性
    private int pageSize=20;//当前页的显示条数

    private String keyword;//查询关键字
    private Integer type = -1;   //条件类别 : 1为大分类  2为小分类

    private Long typeId =-1L;
    private Long productTypeId =-1L;

    private Integer orderBy=-1;//-1为不排序 1为按照销量排序 2为按照价格升序 3为按照价格降序

    /**
     * 当关键字为空和空格时,关键字属性一律输出为空
     * @return
     */

    public String getKeyword() {
        if (keyword==null||"".equals(keyword.trim())){
            return null;
        }
        return keyword;
    }

    public Pageable getPageableWithoutSort(){
        PageRequest of = PageRequest.of(currentPage-1, pageSize);
        return  of;
    }

    public Pageable getPageableAndSort(){
        //PageRequest of=null;
        if (orderBy==null){
            return  PageRequest.of(currentPage-1, pageSize);
        }
        if (orderBy==1){
            return PageRequest.of(currentPage-1,pageSize, Sort.by(Sort.Direction.DESC,"salesNum"));
        }else if(orderBy==2){
            return PageRequest.of(currentPage-1,pageSize, Sort.by(Sort.Direction.ASC,"actualPrice"));
        }else if(orderBy==3){
            return PageRequest.of(currentPage-1,pageSize, Sort.by(Sort.Direction.DESC,"actualPrice"));
        }else {
           return  PageRequest.of(currentPage-1, pageSize);
        }
    }


}
